import time
import traceback

import uiautomator2 as u2


def share(url):
    try:
        # d = u2.connect()

        # 手机内连接
        d = u2.connect('0.0.0.0')
        d.implicitly_wait(120.0)  # 设置元素查找等待时间
        d.unlock()
        time.sleep(1)
        print(d.app_current())
        d.app_stop('org.telegram.messenger')
        time.sleep(2)
        d.app_start('org.telegram.messenger', use_monkey=True)
        time.sleep(6)
        # d.click(0.937, 0.064)
        d(description="Search").click()
        time.sleep(2)
        d.send_keys("🐦比推Bitpush 加密社群", clear=True)
        time.sleep(2)
        d(textContains='🐦比推Bitpush 加密社群', className='android.view.ViewGroup').click()
        time.sleep(2)
        d.send_keys(url, clear=True)
        time.sleep(2)
        d.click(0.929, 0.847)
        time.sleep(2)
        d.app_start("com.termux")
        time.sleep(1)
    except:
        print(traceback.format_exc())
        pass


if __name__ == '__main__':
    share("https://bitpush.news/articles/6331011")
